#include "MyCustomPlot.h"
#include <QMouseEvent>
MyCustomPlot::MyCustomPlot(QWidget *parent)
    : QCustomPlot(parent)
{
    // 创建一个 QCPItemLine 对象
    mVerticalLineLeft = new QCPItemLine(this);
    mVerticalLineRight = new QCPItemLine(this);
    // 设置垂直线的起点和终点
    mVerticalLineLeft->start->setCoords(0, yAxis->range().upper);
    mVerticalLineLeft->end->setCoords(0, yAxis->range().lower);
    mVerticalLineRight->start->setCoords(0, yAxis->range().upper);
    mVerticalLineRight->end->setCoords(0, yAxis->range().lower);
    // 可选：设置线的颜色和宽度
    mVerticalLineLeft->setPen(QPen(Qt::red, 1, Qt::DotLine));
    mVerticalLineRight->setPen(QPen(Qt::green, 1, Qt::DotLine));
}

void MyCustomPlot::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        if (event->modifiers() & Qt::ControlModifier)
        {
            setSelectModel();
        }
        else if(event->modifiers() == Qt::NoModifier)
        {
            // 获取鼠标事件的像素坐标
            double xPixel = event->position().x();
            double yPixel = event->position().y();

            // 将像素坐标转换为图形坐标
            double xCoord = xAxis->pixelToCoord(xPixel);
            // double yCoord = yAxis->pixelToCoord(yPixel);
            setLeftLine(xCoord);
            replot();
        }
    }
    else if (event->button() == Qt::RightButton&& event->modifiers() == Qt::NoModifier)
    {
        // 获取鼠标事件的像素坐标
        double xPixel = event->position().x();
        double yPixel = event->position().y();

        // 将像素坐标转换为图形坐标
        double xCoord = xAxis->pixelToCoord(xPixel);
        setRightLine(xCoord);
        replot();
    }
    QCustomPlot::mousePressEvent(event);
}

void MyCustomPlot::mouseReleaseEvent(QMouseEvent *event)
{
    if (mModel == MouseLeftButtonModel::Drag)
    {
        setSelectModel();
    }
    QCustomPlot::mouseReleaseEvent(event);
}

void MyCustomPlot::paintEvent(QPaintEvent *event)
{
    auto left = mVerticalLineLeft->start->coords().rx();
    mVerticalLineLeft->start->setCoords(left, yAxis->range().upper);
    mVerticalLineLeft->end->setCoords(left, yAxis->range().lower);
    auto right = mVerticalLineRight->start->coords().rx();
    mVerticalLineRight->start->setCoords(right, yAxis->range().upper);
    mVerticalLineRight->end->setCoords(right, yAxis->range().lower);
    QCustomPlot::paintEvent(event);
}

void MyCustomPlot::setSelectModel()
{
    mModel = MouseLeftButtonModel::Select;
    setInteraction(QCP::iRangeDrag, false); // 取消拖动
    setSelectionRectMode(QCP::SelectionRectMode::srmZoom);
}

void MyCustomPlot::setDragModel()
{
    mModel = MouseLeftButtonModel::Drag;
    // 禁用选择矩形
    setSelectionRectMode(QCP::SelectionRectMode::srmNone);
    // 使能拖动
    setInteraction(QCP::iRangeDrag, true);
}

void MyCustomPlot::setLeftLine(double left)
{
    if(left==mVerticalLineLeft->start->coords().rx()){
        return;
    }
    mVerticalLineLeft->start->setCoords(left, yAxis->range().upper);
    mVerticalLineLeft->end->setCoords(left, yAxis->range().lower);
    emit leftLineChanged(left);
}

void MyCustomPlot::setRightLine(double right)
{
    if(right==mVerticalLineRight->start->coords().rx()){
        return;
    }
    mVerticalLineRight->start->setCoords(right, yAxis->range().upper);
    mVerticalLineRight->end->setCoords(right, yAxis->range().lower);
    emit rightLineChanged(right);
}

void MyCustomPlot::setLeftRightLine(double left, double right)
{
    setLeftLine(left);
    setRightLine(right);
}

void MyCustomPlot::setMeasureLineVisible(bool visible)
{
    mVerticalLineLeft->setVisible(visible);
    mVerticalLineRight->setVisible(visible);
}

double MyCustomPlot::leftLine()
{
    return mVerticalLineLeft->start->coords().x();
}

double MyCustomPlot::rightLine()
{
    return mVerticalLineRight->start->coords().x();
}
